//
//  BroadcastData.cpp
//  Simulation
//
//  Created by Christopher Kemsley on 2/27/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#include "BroadcastData.hpp"


using namespace Sim ;



BroadcastInfo :: BroadcastInfo	( Type inType , DeviceID inSender , DeviceID inTarget )
:	type ( inType )
,	senderAddress		( inSender )
,	destinationAddress	( inTarget )
	{
	
	}





ControlBroadcastInfo :: ControlBroadcastInfo	( SubType inType , DeviceID inSender , DeviceID inTarget )
:	BroadcastInfo		( kTypeControl , inSender , inTarget )
,	subType				( inType )
	{
	
	}




void	Sim :: encodeBlock	( unsigned char * buffer , unsigned inMaxLength , DeviceID inSender , DeviceID inTarget )
	{
	if ( sizeof(BroadcastInfo) > inMaxLength )
		ThrowException ( "BroadcastData::encodeBlock" , "Structure is bigger than allowable memory size (" << inMaxLength << ")" , __FILE__,__LINE__ ) ;
	new ((void*)buffer) BroadcastInfo ( BroadcastInfo::kTypeData , inSender , inTarget ) ;
	}
void	Sim :: encodeRTS	( unsigned char * buffer , unsigned inMaxLength , DeviceID inSender , DeviceID inTarget )
	{
	if ( sizeof(ControlBroadcastInfo) > inMaxLength )
		ThrowException ( "ControlBroadcastInfo::encodeRTS" , "Structure is bigger than allowable memory size (" << inMaxLength << ")" , __FILE__,__LINE__ ) ;
	new ((void*)buffer) ControlBroadcastInfo ( ControlBroadcastInfo::kSubTypeRTS , inSender , inTarget ) ;
	}
void	Sim :: encodeCTS	( unsigned char * buffer , unsigned inMaxLength , DeviceID inSender , DeviceID inTarget )
	{
	if ( sizeof(ControlBroadcastInfo) > inMaxLength )
		ThrowException ( "ControlBroadcastInfo::encodeCTS" , "Structure is bigger than allowable memory size (" << inMaxLength << ")" , __FILE__,__LINE__ ) ;
	new ((void*)buffer) ControlBroadcastInfo ( ControlBroadcastInfo::kSubTypeCTS , inSender , inTarget ) ;
	}
void	Sim :: encodeACK	( unsigned char * buffer , unsigned inMaxLength , DeviceID inSender , DeviceID inTarget )
	{
	if ( sizeof(ControlBroadcastInfo) > inMaxLength )
		ThrowException ( "ControlBroadcastInfo::encodeACK" , "Structure is bigger than allowable memory size (" << inMaxLength << ")" , __FILE__,__LINE__ ) ;
	new ((void*)buffer) ControlBroadcastInfo ( ControlBroadcastInfo::kSubTypeACK , inSender , inTarget ) ;
	}

void	Sim :: decodeBroadcast		( unsigned char const * inBuffer , BroadcastInfo::Type & outType , DeviceID & outSender , DeviceID & outTarget )
	{
	BroadcastInfo const & broadcastInfo = * ( (BroadcastInfo*) inBuffer ) ;
	
	outType		=	broadcastInfo . type ;
	outSender	=	broadcastInfo . senderAddress ;
	outTarget	=	broadcastInfo . destinationAddress ;
	}
void	Sim :: decodeControl	( unsigned char const * inBuffer , ControlBroadcastInfo::SubType & outType )
	{
	ControlBroadcastInfo const & BroadcastInfo = * ( (ControlBroadcastInfo*) inBuffer ) ;
	
	outType = BroadcastInfo.subType ;
	}
